$(function () {
  var g_selected_senders = new Array();
  var groups = new Array();

  $("#content").wysihtml5();

  if(g_selected_senders.length == 0)
  {
      $('#selected_area').hide();
  }

  //
  function isObjectValueEqual(a, b)
  {
      var aProps = Object.getOwnPropertyNames(a);
      var bProps = Object.getOwnPropertyNames(b);

      if (aProps.length != bProps.length) {
          return false;
      }

      for (var i = 0; i < aProps.length; i++) {
          var propName = aProps[i];
          if (a[propName] !== b[propName]) {
              return false;
          }
      }
      return true;
  }

  //
  function sender_insert(sender)
  {
      for (var i = g_selected_senders.length - 1; i >= 0; i--)
      {
          if(g_selected_senders[i] == undefined)
          {
             continue;
          }
          if(isObjectValueEqual(g_selected_senders[i],sender))
          {
             // alert("已经存在");
              return false;
          }
      }
      g_selected_senders.push(sender);
      $('#selected_area').show();
      candidates_html_generate();
  }

  //
  function candidates_empty_check()
  {
    for (var i = g_selected_senders.length - 1; i >= 0; i--)
    {
        if(g_selected_senders[i] != undefined)
        {
            return false;
        }
    }
    return true;
  }

  //
  function candidates_html_generate()
  {
      $('#sender_candidates').html("");
      $.each(g_selected_senders, function (i, item) {
          if(item != undefined)
          {
            var id = item.type+"#"+item.id;
            $('#sender_candidates').append('<div class="col-md-4"><input class="candidate_item" name="candidate_item[]" type="checkbox" checked=checked value="'+id+'">'+item.name+'</div>');
          }
      });
  }

  //
  function sender_delete(sender)
  {
      for (var i = g_selected_senders.length - 1; i >= 0; i--)
      {
          if(g_selected_senders[i] == undefined)
          {
             continue;
          }

          if(isObjectValueEqual(g_selected_senders[i],sender))
          {
              delete g_selected_senders[i];
          }
      }
      //
      if(candidates_empty_check())
      {
          $('#selected_area').hide();
      }
      else
      {
          candidates_html_generate();
      }
  }

  //
  function get_group_name(g_id)
  {
      for (var i = groups.length - 1; i >= 0; i--)
      {
          if(groups[i].id == g_id)
          {
              return groups[i].name;
          }
      }
      return "";
  }

  $('#notcie_receiver_type').change(function(){
      $('.sender_select_area').hide();
      //g_selected_senders = [];
      if($(this).val() == 1)
      {
          var sender = {type:1, id:"ALL", name:"辖内所有安监部门"};
          sender_insert(sender);
      }

      if($(this).val() == 3)
      {
        var sender = {type:3, id:"ALL", name:"辖内所有市级安监部门"};
        sender_insert(sender);
      }

      if($(this).val() == 4)
      {
        var sender = {type:4, id:"ALL", name:"辖内所有县级安监部门"};
        sender_insert(sender);
      }

      if($(this).val() == 9)
      {
          $('#group_select_area').show();
          $.get($('#base_url').val()+"government/oa/ajax_load_groups", function(respond){
              eval("data="+respond);

              if(groups.length == 0)
              {
                  groups = data;
              }

              $('#group_text_id').html("");
              $.each(data, function (i, item) {
                  $('#group_text_id').append('<div class="col-md-4"><input class="group_item" type="checkbox" value="'+item.id+'">'+item.name+'</div>');
              });
          });
      }
      if($(this).val() == 13)
      {
        $('#gov_select_area').show();
      }
      if($(this).val() == 6)
      {
        $('#company_select_area').show();
      }
      
      //NEW ADD
      if($(this).val() == '6ALL')
      {
	        var sender = {type:6, id:"ALL", name:"辖内所有企业"};
	        sender_insert(sender);
      }
  });

  //
  $('#group_text_id').on('click', '.group_item' ,function(){
      var sender = {type:9, id:$(this).val(), name:get_group_name($(this).val())};
      if(this.checked === true)
      {
          sender_insert(sender);
      }
      else
      {
         sender_delete(sender);
      }
  });

  //
  $('#gov_search').autocomplete({
      serviceUrl: $('#base_url').val()+"government/oa_process/search_gov/",
      onSelect: function (suggestion) {
          sender_insert({type:suggestion.type, id:suggestion.data, name:suggestion.value});
      }
  });

  $('#company_search').autocomplete({
      serviceUrl: $('#base_url').val()+"project_manage/select_company/",
      onSelect: function (suggestion) {
          sender_insert({type:6,id:suggestion.data,name:suggestion.value});
      }
  });


  $('#company_name_search').autocomplete({
      serviceUrl: $('#base_url').val()+"government/two_class/search_company/",
      onSelect: function (suggestion) {
          source: suggestion.value;
      }
  });
});
